package com.mark.web.common.controller;

import com.mark.common.ServerResponse;
import com.mark.web.common.service.IUploadFilesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;

/**
 * @Description: 通用文件上传类
 * @Author: Kingsley
 * @CreateDate: 2018/9/26 22:10
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@RestController()
@RequestMapping("/common/image/")
public class FileUploadController {

    /**
     * //图片的的角色【user..】
     */
    private static final String ROLE_IMAGES = "role";
    /**
     * 角色的id
     */
    private static final String ROLE_ID = "roleId";


    private final IUploadFilesService iUploadFilesService;

    @Autowired
    public FileUploadController(IUploadFilesService iUploadFilesService) {
        this.iUploadFilesService = iUploadFilesService;
    }


    @PostMapping(value = "upload.do")
    public ServerResponse uploadFile(@RequestParam(value = "files", required = false) MultipartFile[] files, ServletRequest request) {
        //文件校验、上传类型的校验，上传类型的id校验
        String role = request.getParameter(ROLE_IMAGES);
        String roleId = request.getParameter(ROLE_ID);
        if (files.length == 0) {
            return ServerResponse.createByErrorMessage("上传的文件不能为空");
        }
        if (StringUtils.isBlank(role)) {
            return ServerResponse.createByErrorMessage("上传图片的所属的类型不能为空");
        }
        if(StringUtils.isBlank(roleId)){
            return ServerResponse.createByErrorMessage("类型的id不能为空");
        }
        return iUploadFilesService.uploadFiles(files,role,roleId);
    }

}
